package leetcode.slidingwindow;

import java.util.Arrays;

//209. 长度最小的子数组
public class MinSubArrayLen {
    public static void main(String[] args) {
        int[] data = {2, 3, 1, 2, 4, 3};
        System.out.println(minSubArrayLenP(7, data));
    }

    //滑动窗口
    public static int minSubArrayLen(int target, int[] nums) {
        int sum = 0;
        int right = 0;
        int left = 0;
        int n = nums.length;
        int res = Integer.MAX_VALUE;
        while (right < n) {
            int x = nums[right];
            right++;
            if (x > target && x == nums[0]) {
                left = right;
                sum = 0;
                res = 1;
                continue;
            }
            if (x > target) {
                left = right;
                sum = 0;
                continue;
            }
            sum += x;
            while (sum >= target) {
                res = Math.min(res, right - left);
                sum -= nums[left];
                left++;
            }


        }
        return res == Integer.MAX_VALUE ? 0 : res;
    }

    //滑动窗口官方
    public static int minSubArrayLen12(int target, int[] nums) {
        int sum = 0;
        int left = 0;
        int right = 0;
        int res = Integer.MAX_VALUE;
        while (right < nums.length) {
            int x = nums[right];
            sum += x;
            while (sum >= target) {
                res = Math.min(res, right - left + 1);
                sum -= nums[left];
                left++;
            }
            right++;
        }
        return res == Integer.MAX_VALUE ? 0 : res;
    }

    //暴力
    public static int minSubArrayLen2(int target, int[] nums) {

        int res = Integer.MAX_VALUE;
        for (int i = 0; i < nums.length; i++) {
            int sum = 0;
            for (int j = i; j < nums.length; j++) {
                sum += nums[j];
                if (sum >= target) {
                    res = Math.min(res, j - i + 1);
                    break;
                }
            }
        }
        return res == Integer.MAX_VALUE ? 0 : res;
    }


    // 二战--暴力
    public static int minSubArrayLenP(int target, int[] nums) {
        long count = Arrays.stream(nums).filter(item -> item >= target).count();
        if (count > 0) {
            return 1;
        }
        int res = Integer.MAX_VALUE;
        for (int i = 0; i < nums.length; i++) {
            int sum = nums[i];
            for (int j = i + 1; j < nums.length; j++) {
                sum += nums[j];
                if (sum >= target) {
                    res = Math.min(res, j - i + 1);
                    break;
                }
            }
        }
        return res == Integer.MAX_VALUE ? 0 : res;
    }

    // 滑动窗口
    public static int minSubArrayLenP2(int target, int[] nums) {
        int left = 0;
        int sum = 0;
        int n = nums.length;
        int right = 0;
        int res = Integer.MAX_VALUE;
        while (right < n) {
            sum += nums[right];
            while (sum >= target) {
                res = Math.min(res, right - left + 1);
                sum -= nums[left++];
            }
            right++;
        }
        return res == Integer.MAX_VALUE ? 0 : res;
    }
}
